/**
 * Created by JLB1 on 2017/2/7.
 */

import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import AJAX from '../../lib/ajax';
import Toast from "../MyPackage/components/Toast"

require('../../styles/common.less');
require('../../styles/lux.less');
require('../../styles/resetWeui.less');
require('./DaosuLogisId.less');

class DaosuLogisId extends Component {

  state = {
    logisId: '',
  };

  componentDidMount() {
    AJAX.request('get_logis_id', {}, data => {
      this.setState({
        logisId: data.body || '',
      });
    });
  }

  changeLogisId = e => {
    this.setState({
      logisId: e.target.value,
    })
  }

  handleSubmit = () => {
    let { logisId } = this.state;
    logisId = logisId.replace(/[\r\n]/g, '').trim();
    if (logisId.charAt(logisId.length - 1) == ',') {
      logisId = logisId.substring(0, logisId.length - 1);
    }

    AJAX.request('save_logis_id', { logis_id: logisId }, data => {
      if (data.code == 0) {
        Toast.success({ content: '保存成功' });
      }
    })
  }

  render() {
    const { logisId } = this.state;

    return (
      <div>
        <div className="code-content">
          <div className="title-content">
            <h1>到宿开通学校配置</h1>
            <h1>（物流中心）</h1>
            <h3>(多个用逗号隔开)</h3>
          </div>

          <div className="code-box">
            <textarea rows="15" value={logisId} onChange={this.changeLogisId}></textarea>
          </div>
        </div>

        <div className="footer-box">
          <div className="footer-btn-box">
            <button
              className="footer-btn"
              onClick={this.handleSubmit}
            >
              提交
            </button>
          </div>
        </div>
      </div>
    );
  }
}

ReactDOM.render(<DaosuLogisId />, document.getElementById('app'));

if (module.hot) {
  module.hot.accept();
}